﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef  Resource_Maneger_h
#define  Resource_Maneger_h

#include <windows.h>
#include <stdio.h>
#include "..\\import\\CRLEngi_strings_lib\\CRLEngi_strings.h"
#include "..\\import\\param_trees_lib\\param_trees.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class CLREngi_Resource_Maneger
{
private:
	typedef struct msh_NODE_TYP{     //для мешей     
		   unsigned int    id;                          // Идентификатор
		   LPD3DXMESH      *pMesh;
           CRLEngi_string   filename;                   // путь
		   msh_NODE_TYP        *next;                   // Указатель на следующий узел списка
	    }msh_NODE,*msh_NODE_PTR;

public:
unsigned int            LOADED_meshes;   // число объектов
msh_NODE_PTR            msh_head;        // начало списка объектов

//-----------------------------------------------------------------------------
// Name: Load()
// Загружаем ресурс
//-----------------------------------------------------------------------------
void LoadFile(CRLEngi_string file_name,LPD3DXMESH  *pMesh,LPD3DXBUFFER *pD3DXMtrlBuffer)
{
	msh_NODE_PTR new_node = NULL;
	msh_NODE_PTR curr;
	//---------------------------------------------------
	// Проверяем не загружен ли уже ресурс с таким именем
	//---------------------------------------------------
	if (msh_head != NULL){
	    curr = 	msh_head;
		while (curr!=NULL){
			if (curr->filename == file_name){
				pMesh = curr->pMesh;
				return;
			    }
			curr = curr->next;
		  }
	   } 
	//---------------------------------------------------
	//Если ресурс не был загружен ранее, то грузим его
	//---------------------------------------------------
	if (msh_head == NULL) //Список пуст
				 curr = NULL; //(*head);
			else
			     curr = (msh_head); 
			// Создание нового узла
			// В C++ здесь используется оператор new
			new_node = (NODE_PTR)malloc(sizeof(NODE));
			//Заполняем поля
			LOADED_meshes             += 1;

    		new_node->filename = new Static_CObject(matWorld,filename.getChars(),d3dDevice,&ResManager);
			new_node->Object->InitParameters();
		
			new_node->filename   = filename;
			new_node->id         = LOADED_objects;
			new_node->next       = NULL; // Лучше это делать всегда;
			//  if (MakeCurrient) CurrientCamera = new_node;
			// в данном случае наш узел будет
			// последним в списке, так что это
			// просто необходимо
			// Поиск конца списка и вставка
			if (curr == NULL){ //Список пуст
				(head) = new_node;
				return(new_node->id);
				}//if
			else{
				while(curr->next != NULL) curr  = curr->next;
				curr->next = new_node;
				new_node->prev = curr;
				return(new_node->id);
				}//else
			*/
}

//-----------------------------------------------------------------------------
// Name: DestroyRes()
// Выгружаем ресурс
//-----------------------------------------------------------------------------
void DestroyRes()
{

}


ResourceManeger(void)
{
	msh_head = NULL;
}

~ResourceManeger(void)
{
}


};


	





#endif 